package com.nulldev.util.networking.npacket4.iprotocols.ncryptx;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import com.nulldev.util.networking.npacket4.nPacket;
import com.nulldev.util.networking.npacket4.extensions.PacketExtension;

public class nCryptCipherShareMeta extends nPacket {

	private static final long serialVersionUID = 2546306130021864554L;
	private CipherOption[] supportedCiphers;
	private byte cryptVersion = nCryptX.CRYPT_VERSION;

	public nCryptCipherShareMeta(final nPacket packet) {
		super("ncryptx.cipher_meta", packet);
	}

	public nCryptCipherShareMeta(final CipherOption[] supportedMethods, final PacketExtension... exts) {
		super("ncryptx.cipher_meta", exts);
		this.supportedCiphers = supportedMethods;
	}

	@Override
	public void onGenerate(final DataOutputStream dos) throws IOException {
		dos.writeByte(this.cryptVersion);
		dos.writeInt(this.supportedCiphers.length);
		for (final CipherOption m : this.supportedCiphers)
			dos.writeShort(m.id());
		dos.flush();
	}

	@Override
	public void onPacket(final DataInputStream dis) throws IOException {
		final byte cryptAPIVersion = dis.readByte();
		final int length = dis.readInt();
		final CipherOption[] m = new CipherOption[length];
		for (int i = 0; i < length; i++) {
			m[i] = CipherOption.get(dis.readShort());
		}
		this.supportedCiphers = m;
		this.cryptVersion = cryptAPIVersion;
	}

	public CipherOption[] getCiphers() {
		return this.supportedCiphers;
	}

	public byte getCryptVersion() {
		return this.cryptVersion;
	}
}
